package io.renren.modules.commonproblem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.utils.GreenTextScan;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;
import io.renren.modules.commonproblem.entity.Problem;
import io.renren.modules.commonproblem.service.ProblemService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


@RestController
@RequestMapping("/problem")
public class ProblemController {

    @Autowired
    ProblemService problemService;

    @GetMapping("/findProblemList")
    @RequiresPermissions("app:problem:list")
    public R findProblemList(@RequestParam Map<String, Object> params){
        PageUtils page= problemService.findProblemList(params);
        return R.ok().put("page",page);
    }

    /**
     * 查询详细信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("app:problem:info")
    public R info(@PathVariable("id") Integer id) {
        Problem problem = problemService.getById(id);
        return R.ok().put("problem", problem);
    }


    /**
     * 新增
     */
    @RequestMapping("/save")
    @RequiresPermissions("app:problem:save")
    public R save(@RequestBody Problem problem) {
       //TODO 内容oss审核
        if (StringUtils.isNotBlank(problem.getProblemContent())){
            try {
                Map map = new GreenTextScan().greeTextScan(problem.getProblemContent());

                // 审核失败
                if (map.get("suggestion").equals("block")) {
                    return R.error(402,"阿里云审核-文章图片违规");
                }
                // 人工审核
                if (map.get("suggestion").equals("review")) {
                    return R.error(401,"阿里云异常，需要人工审核");
                }

                //审核通过
                problemService.save(problem);
                return R.ok();

            } catch (Exception e) {
                e.printStackTrace();
                return R.error(400,"阿里云审核失败，转入人工审核");
            }
        }
        return R.error(403,"系统正忙...");
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("app:problem:update")
    public R update(@RequestBody Problem problem) {
        //TODO 内容oss审核
        if (StringUtils.isNotBlank(problem.getProblemContent())){
            try {
                Map map = new GreenTextScan().greeTextScan(problem.getProblemContent());

                // 审核失败
                if (map.get("suggestion").equals("block")) {
                    return R.error(402,"阿里云审核-文章图片违规");
                }
                // 人工审核
                if (map.get("suggestion").equals("review")) {
                    return R.error(401,"阿里云异常，需要人工审核");
                }
                problemService.updateById(problem);
                return R.ok();

            } catch (Exception e) {
                e.printStackTrace();
                return R.error(400,"阿里云审核失败，转入人工审核");
            }
        }
        return R.error(403,"系统正忙...");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("app:problem:delete")
    public R delete(@RequestBody Integer[] ids) {
        problemService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }
}
